import Vue from 'vue'
import VueRouter from 'vue-router'
import axios from "../axios";
import store from "../store"
import Login from "@/views/Login";
import Home from "@/views/Home";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: '/index',
    children: [
      {
        path: '/index',
        name: 'Index',
        meta: {
          title: "首页"
        },
        component: () => import('@/views/Index.vue')
      },
      {
        path: '/userCenter',
        name: 'UserCenter',
        meta: {
          title: "个人中心"
        },
        component: () => import('@/views/UserCenter.vue')
      },
      {
        path: '/resetPass',
        name: 'ResetPass',
        meta: {
          title: "修改密码"
        },
        component: () => import('@/views/ResetPass.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("token")
  if (to.path == '/login') {
    // 如果是登录请求，通过
    console.log("登录")
    next()
  } else if (!token) {
    // 如果token为空，跳转到登录页面
    next({path: '/login'})
  }
  next()
})



export default router
